/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.dataImport.r9.bean;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.bean.EconomicBean;
import com.yunhoo.base.model.Economic;
import com.yunhoo.dataImport.r9.model.EcoAndConfig;
import com.yunhoo.dataImport.r9.model.RemoteItem;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.FacesUtils;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

@ManagedBean(name = "ecoDataInterfaceBean")
@SessionScoped
public class EconomicConfig {

    private Economic eco = null;
    private TreeNode rootNode = null;
    private List<RemoteItem> datali = null;
    /*
     * 登录单位 和 会计年度 -- 查询参数
     */
    private String orgCode = null;
    private Integer iyear = null;

    private void loadTree() {
        if (rootNode == null) {
            rootNode = new TreeNodeImpl();
            EconomicBean ecoBean = (EconomicBean) FacesUtils.getManagedBean("economicBean");
            rootNode = ecoBean.getRootNode();
        }
    }

    public void selected(TreeSelectionChangeEvent event) {
        eco = (Economic) TreeUtil.listener(event);
        if (eco.getIsEnd() == 1) {
            initList();
        } else {
            datali = null;
        }
    }

    private void initList() {
        Map map = new HashMap();
        map.put("orgCode", LoginOrgCode());
        map.put("iyear", loginYear());
        map.put("ecoCode", eco.getCode());
        datali = IbatisTemplate.queryForList("r9DataImport.queryList_remoteItem_eco", map);
    }

    public void reSelect() {//重选
        for (RemoteItem data : datali) {
            if (data.isSelected()) {
                data.setSelected(false);
            }
        }
    }

    //保存配置数据
    public void save() {
        if (datali == null) {
            return;
        }
        String ecoCode = eco.getCode();


        List<EcoAndConfig> insertli = new ArrayList<EcoAndConfig>();
        for (RemoteItem item : datali) {
            if (item.isSelected()) {
                EcoAndConfig data = new EcoAndConfig(true);
                data.setEcoCode(ecoCode);
                data.setConfigCode(item.getCode());
                insertli.add(data);
            }
        }
//        if (insertli.isEmpty()) {
//            insertli  为空时 删除当前配置；
//            return;
//        }
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();
            client.startBatch();
            EcoAndConfig deleteParam = new EcoAndConfig(true);
            deleteParam.setEcoCode(ecoCode);
            client.delete("r9DataImport.deleteEcoConfig", deleteParam);
            for (EcoAndConfig data : insertli) {
                client.insert("r9DataImport.insertEcoConfig", data);
            }
            client.executeBatch();
            client.commitTransaction();
        } catch (SQLException ex) {
            try {
                client.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(EconomicConfig.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(EconomicConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        initList();
    }

    public TreeNode getTreeNode() {
        if (this.rootNode == null) {
            loadTree();
        }
        return rootNode;
    }

    public Integer loginYear() {
        return iyear == null ? LoginParam.getIyear() : iyear;
    }

    public String LoginOrgCode() {
        return orgCode == null ? LoginParam.getOrg().getCode() : orgCode;
    }

    public Economic getEco() {
        return eco;
    }

    public void setEco(Economic eco) {
        this.eco = eco;
    }

    public List<RemoteItem> getDatali() {
        return datali;
    }

    public void setDatali(List<RemoteItem> datali) {
        this.datali = datali;
    }
}
